﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using ePlanet.ArchiveMange.Services;
using ePlanet.ArchiveMange.Models;
using System.Data;

namespace Web.Views.ArchiveManage
{
    public partial class ArchiveMenuManageEdit : PageBase
    {
        [Dependency]
        public IArchiveMenuService MenuService { get; set; }

        /// <summary>
        /// 请求变量
        /// </summary>
        public string Rre_Role_ID
        {
            get
            {
                if (Request.QueryString["Role_ID"] != null)
                {
                    return Request.QueryString["Role_ID"];
                }
                else
                {
                    return null;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCeckBoxList();
                CheckArchiveMenus();
            }
        }

        /// <summary>
        /// 绑定绑定复选框列
        /// </summary>
        private void BindCeckBoxList()
        {
            cblArchiveMenu.DataSource = LoadData();
            cblArchiveMenu.DataTextField = "ARCHIVEMENU";
            cblArchiveMenu.DataValueField = "TYPEID";
            cblArchiveMenu.DataBind();
        }


        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {

            List<string> list = new List<string>();
            foreach (ListItem item in cblArchiveMenu.Items)
            {
                if (item.Selected == true)
                {
                    list.Add(item.Value);
                }
            }
            //先清空所有的编目，重新插入
            ClearRoleArchive(Rre_Role_ID);
            DataSet ds = new DataSet();
            ArchiveMangeModal.ARCHIVEMENUFUNCDataTable dt = new ArchiveMangeModal.ARCHIVEMENUFUNCDataTable();
                ArchiveMangeModal.ARCHIVEMENUFUNCRow row = null;
            foreach (string archiveid in list)
            {
                row = dt.NewARCHIVEMENUFUNCRow();
                //新增状态
                row.REC_ID = Guid.NewGuid().ToString();
                row.ROLE_ID = Rre_Role_ID;
                row.ARCHIVEMENUID = int.Parse(archiveid);
                row.CREATETIME = DateTime.Now;
                row.CREATEUSER = this.CurrentUser.User_ID;
                row.SetLASTUPDATETIMENull();
                row.SetLASTUPDATEUSERNull();
                dt.Rows.Add(row);
            }
            ds.Tables.Add(dt);
            MenuService.SaveData(ds);
            Response.Redirect("ArchiveMenuMange.aspx");
        }

        /// <summary>
        /// 返回按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ArchiveMenuMange.aspx");
        }


        #region 页面方法
        private void ClearRoleArchive(string role_id)
        {
            string sql = "delete from   archivemenufunc  where   role_id='"+role_id+"'";
            IMDataHelper dal = new IMDataHelper();
            dal.DBHelper.ExecuteSql(sql);
        }


        /// <summary>
        /// 更新选中状态
        /// </summary>
        private void CheckArchiveMenus()
        {
            DataSet ds = LoadRoleArchive(Rre_Role_ID);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    foreach (ListItem item in cblArchiveMenu.Items)
                    {
                        if (item.Value == row["archivemenuid"].ToString())
                        {
                            item.Selected = true;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 加载所有的基本档案编目
        /// </summary>
        private DataTable LoadData()
        {
            ArchiveMangeModal.ARCHIVEMENUDataTable dt = MenuService.GetAllBaseMenu();
            return dt;
        }

        private DataSet LoadRoleArchive(string role_id)
        {
            try
            {
                string sql = "select * from archivemenufunc t where t.role_id='" + role_id + "'";
                IMDataHelper dal = new IMDataHelper();
                DataSet ds = dal.DBHelper.ExecuteDataSet(sql);
                return ds;
            }
            catch
            {
                return null;
            }
        }
        #endregion
    }
}